package com.boruishangge.common;


import com.boruishangge.common.config.KeyAndValueAppPrefs;
import com.boruishangge.common.entiy.Loginbean;
import com.boruishangge.common.http.RetrofitFactory;
import com.boruishangge.common.uitls.SPUtils;
import com.boruishangge.persagysdk.base.AppApplicationContext;
import com.boruishangge.persagysdk.base.BaseConstant;
import com.boruishangge.persagysdk.uitls.StringUtils;
import com.google.gson.reflect.TypeToken;
import com.tencent.mmkv.MMKV;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

/**
 * TODO 用于存放  公共的数据获取
 */
public class APlushConstant {


//    public static String ServerImageDown = "http://" + BaseConstant.  + "/image-service/common/file_get?systemId=" + Constant
//            .system_code + "&key=";
    public  static  String mmkv_login_account="mmkv_login_account";
    /**
     * TODO 获取最近登陆成功的账户 且以倒序 排列
     * @return
     */
    public  static List<Loginbean> getLoginAccountList(){
        List<Loginbean>     list=new ArrayList<>();
        String json= SPUtils.getInstance().getString(mmkv_login_account);
        if (!StringUtils.checkNull(json)){
            list.addAll(RetrofitFactory.getGson().fromJson(json,new TypeToken<List<Loginbean>>(){}.getType()));
            Collections.reverse(list);
        }
        return  list;
    }


    /**
     * TODO 存放当前最近登陆成功的账户 到本地MMKV
     * @param loginbean
     */
    public  static  void  setLoginAccount(Loginbean loginbean){
        List<Loginbean>     list=new ArrayList<>();
        String json=SPUtils.getInstance().getString(mmkv_login_account);
        if (StringUtils.checkNull(json)){
            //TODO 代表是第一次
            list.clear();
            list.add(loginbean) ;
        }else {
            list.addAll(RetrofitFactory.getGson().fromJson(json,new TypeToken<List<Loginbean>>(){}.getType()));
            Iterator<Loginbean> iterator = list.iterator();
            String userId2=  loginbean.getUserId();
            while (iterator.hasNext()){
                String userId=  iterator.next().getUserId();
                if (!StringUtils.checkNull(userId)&&!StringUtils.checkNull(userId2)&&userId2.equals(userId)){
                    iterator.remove(); //TODO  删除掉重复
                }
            }
            list.add(loginbean);
        }
        SPUtils.getInstance().put(mmkv_login_account, RetrofitFactory.getGson().toJson(list));
    }


    /**
     * TODO 清除所有登陆的用户账户数据
     */
    public  static  void  clearListAccount(){
        SPUtils.getInstance().put(mmkv_login_account,  "");
    }




    /**
     *TODO  判断是否登陆
     */
    public static boolean isLogin() {
        if (StringUtils.checkNull(SPUtils.getInstance(AppApplicationContext.currentUserPrefsName).getString(KeyAndValueAppPrefs.Key.CASH_TOKEN))
                && StringUtils.checkNull( SPUtils.getInstance(AppApplicationContext.currentUserPrefsName).getString(KeyAndValueAppPrefs.Key.CUSTOMER_ID))) {
            return false;
        } else {
            return true;
        }
    }


    /**
     * TODO 获取当前登陆用户Id
     * @return
     */
    public  static  String getCustomerId(){
        return  SPUtils.getInstance(AppApplicationContext.currentUserPrefsName).getString(KeyAndValueAppPrefs.Key.CUSTOMER_ID);
    }




    /**
     * TODO 获取当前登陆用户userName
     * @return
     */
    public  static  String getuserName(){
        return  SPUtils.getInstance(AppApplicationContext.currentUserPrefsName).getString(KeyAndValueAppPrefs.Key.CASH_UserName);
    }



    /**
     * TODO 获取当前登陆用户Tocken
     * @return
     */
    public  static  String getTocken(){
        return  SPUtils.getInstance(AppApplicationContext.currentUserPrefsName).getString(KeyAndValueAppPrefs.Key.CASH_TOKEN);
    }
    /**
     * TODO 获取当前登陆用户信息
     * @return
     */
    public  static  String getUserInfo(){
        return  SPUtils.getInstance(AppApplicationContext.currentUserPrefsName).getString(KeyAndValueAppPrefs.Key.SP_KEY_LOGIN_RESULT);
    }
    public  static  String getWebUrl(){
        return  SPUtils.getInstance(AppApplicationContext.currentUserPrefsName).getString(KeyAndValueAppPrefs.Key.URl);
    }
    public  static  void setWebUrl(String text){
        if (!StringUtils.checkNull(text)){
            SPUtils.getInstance(AppApplicationContext.currentUserPrefsName).put(KeyAndValueAppPrefs.Key.URl,text);
        }
    }


    /**
     * TODO 返回图片Url
     * @return
     */
    public static  String getHeadUrl(String key){


        return  BaseConstant.ImageIP+"/image-service/common/file_get?systemId=dataPlatform&key="+key;
    }
}
